package com.chance.cc.crawler.development.command.job.domain.vm.pcauto.simple;

import com.alibaba.fastjson.JSON;
import com.chance.cc.crawler.core.CrawlerEnum;
import com.chance.cc.crawler.core.CrawlerJob;
import com.chance.cc.crawler.core.downloader.HttpConfig;
import com.chance.cc.crawler.core.downloader.HttpPage;
import com.chance.cc.crawler.core.filter.FilterUtils;
import com.chance.cc.crawler.core.record.CrawlerRecord;
import com.chance.cc.crawler.core.record.CrawlerRequestRecord;
import com.chance.cc.crawler.development.command.job.domain.vm.pcauto.PcautoCommonCrawlerSchedulerJob;
import com.chance.cc.crawler.meta.core.bean.CrawlerMetaConstant;
import com.chance.cc.crawler.meta.core.bean.job.CrawlerScheduleJob;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.List;

import static com.chance.cc.crawler.core.CrawlerEnum.CrawlerRecordFilter.dateRange;
import static com.chance.cc.crawler.core.CrawlerEnum.CrawlerRequestType.turnPage;
import static com.chance.cc.crawler.development.scripts.allfeild.AICCommonField.Field_Path;

/**
 * @Author Zhao.Hhuan
 * @Date Create in 2021/3/5 11:47
 * @Description
 *      太平洋号 板块
 **/
public class PcautoWebHjCoreSimple1CrawlerSchedulerJob extends PcautoCommonCrawlerSchedulerJob {
    private static final String crawler_level = "simple";

    public static void main(String[] args) {
        publishCrawlerScheduleJobInfo();
    }

    public static CrawlerJob publishCrawlerScheduleJobInfo(){
        CrawlerJob crawlerJob = crawlerSchedulejob();

        //发布定时采集作业
        CrawlerScheduleJob crawlerScheduleJob = new CrawlerScheduleJob();
        crawlerScheduleJob.setDomain(domain);
        crawlerScheduleJob.setCrawlerJob(JSON.toJSONString(crawlerJob));
        crawlerScheduleJob.setJobType(CrawlerMetaConstant.ScheduleCrawlerJobType.crawler.enumVal());
        crawlerScheduleJob.setNote("太平洋号板块临时回溯采集");
        crawlerScheduleJob.setCrawlerKey(crawlerJob.generateCrawlerKey());
        HttpPage page = crawlerJobCommand.addOrUpdateCrawlerScheduleJob(crawlerScheduleJob);
        System.out.println("发布crawler作业：" + page.getRawText());
        return crawlerJob;

    }

    public static CrawlerJob crawlerSchedulejob(){
        String url = "https://hj.pcauto.com.cn/api/articles/tag/1?pNo=2551&pSize=10&sortKey=0";
        String site = "hj";
        String siteBiz = "simple";//正式上线之后再加,真正的site_biz = "hao_video/article-simple
        CrawlerRequestRecord crawlerRequestRecord = CrawlerRequestRecord.builder()
                .startPageRequest(domain, turnPage)
                .domain(domain)
                .httpUrl(url)
                .httpConfig(HttpConfig.me(domain))
                .filter(dateRange)
                .addFilterInfo(FilterUtils.dateRangeFilterInfo(0,new long[]{1608566400000L,System.currentTimeMillis()}))
                .releaseTime(System.currentTimeMillis())
                .resultLabelTag(CrawlerEnum.CrawlerDataType.article)
                .resultLabelTag(CrawlerEnum.CrawlerDataType.interaction)
                .resultLabelTag(CrawlerEnum.CrawlerDataType.comment)
                .proxy(proxy)
                .build();
        crawlerRequestRecord.tagsCreator().bizTags().addDomain(domain);
        crawlerRequestRecord.tagsCreator().bizTags().addSite(site);
        crawlerRequestRecord.tagsCreator().bizTags().addSiteBiz(siteBiz);
        List<String> modulePath = Arrays.asList("海选导购");
        crawlerRequestRecord.tagsCreator().bizTags().addCustomKV(Field_Path, modulePath);

        CrawlerRecord commentFilter = new CrawlerRequestRecord();
        commentFilter.setFilter(dateRange);
//        commentFilter.addFilterInfo(FilterUtils.redisFilterKeyInfo(StringUtils.joinWith("-","filter",domain,site,crawler_level,"queue")));
        commentFilter.addFilterInfo(FilterUtils.dateRangeFilterInfo(0,new long[]{1608566400000L,System.currentTimeMillis()}));
        crawlerRequestRecord.tagsCreator().resultTags().getCategoryTag().addKVTag("comment_filter_info", JSON.toJSONString(commentFilter));

        return CrawlerJob.builder()
                .crawlerJobThreadNumber(15)
                .triggerInfo(
                        domain,
                        CrawlerMetaConstant.ScheduleJobTrigger_Simple,
                        System.currentTimeMillis(),
                        StringUtils.joinWith("-",site,crawler_level,"1",CrawlerMetaConstant.ScheduleJobTriggerJob_Realtime))
                .crawlerRequestQueue(CrawlerMetaConstant.redisRequestQueue(StringUtils.joinWith("-","crawler",domain,site,crawler_level,"1","queue")))
//                .fileResultPipeline(null, "/data/chance_crawler_test/data/pcauto/pcauto_module_simple.log", false)
                .kafkaResultPipeline(null,kafkaTopciForRealTimeJob,null)
                .requestRecord(crawlerRequestRecord)
                .build();
    }
}
